package com.china.great.system.service.impl;

import com.china.great.system.entity.SystemLoginUser;
import com.china.great.system.service.SystemUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

/**
 * 实现了DetailsService接口，用来做登陆验证
 * 登陆身份认证
 * @author: LiuHongYang
 * createAt: 2020/5/14
 */
@Component(value="CustomUserDetailsService")
public class CustomUserDetailsServiceImpl implements UserDetailsService {
    private static final Logger logger = LoggerFactory.getLogger(SystemUserServiceImpl.class);
   // private final UserMapper authMapper;
    @Autowired
    private SystemUserService systemUserService;
    /*@Autowired
    private SystemRoleService systemRoleService;*/

    /*public CustomUserDetailsServiceImpl(UserMapper authMapper, RoleMapper roleMapper) {
        this.authMapper = authMapper;
        this.roleMapper = roleMapper;
    }*/

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SystemLoginUser userDetail = systemUserService.getUser(username);
        logger.info("4================userDetail的值是："+userDetail.toString());
        if (userDetail == null) {
            throw new UsernameNotFoundException(String.format("No userDetail found with username '%s'.", username));
            //Collection<GrantedAuthority> authList = getAuthorities();
            //SystemLoginUser userDetail = new SystemLoginUser("amos","123456",authList);

        }

        /**  * 获取用户的角色权限,为了降低实验的难度，这里去掉了根据用户名获取角色的步骤     * @param    * @return   */
    /*private Collection<GrantedAuthority> getAuthorities(){
        List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>();
        authList.add(new SimpleGrantedAuthority("ROLE_USER"));
        authList.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        return authList;
    }*/
       /* Role role = systemRoleService.getRole(userDetail.getId());
        userDetail.setRoles(role);*/
        return userDetail;
    }
}